import un from "@uni-helper/uni-network"
//创建axios实例
const instance = un.create({
	baseUrl: "https://gmall-prod.atguigu.cn/skb",
	timeout: 1000
})
instance.interceptors.request.use(config => {
	uni.showLoading({
			title: "数据加载中...",
			mask: true
		})
		const token = uni.getStorageSync("token")
		console.log(token)
		config.headers={}
		if (token && config.headers) {
			Object.assign(config, {
				headers: {
					token
				}
			})
		}
		console.log(config)
		return config
}, (Error) => {
	return Promise.reject(Error)
})
instance.interceptors.response.use(response => {
		uni.hideLoading()
		const res = response.data
		if (res.code !== 200) {
			if (res.code !== 28004) {
				uni.showToast({
					title: res.message || "error",
					duration: 5000,
					icon: "error"
				})
			} else {
				uni.showToast({
					title: "身份认证过期,请重新登录！",
					duration: 5000,
					icon: "error"
				})
				uni.clearStorageSync()
			}
			return Promise.reject(new Error(res.message || "Error"))
		} else {
			return res
		}

}, Error => {
	return Promise.reject(Error)
})
export default instance
